<?php namespace Admin\Controller; 

use Admin\AuthController as Controller;

class HtmlController extends Controller
{

	//生成首页
	public function CreateHome()
	{
		if(IS_POST)
		{
			//缓冲区
			ob_start();
			$obj=new \Home\Controller\IndexController;
			$obj->index();
			$content=ob_get_clean();
			if(file_put_contents('index.html', $content))
			{
				$this->success('生成首页成功');
			}
			else
			{
				$this->error('请修改 / 目录权限');
			}
		}
		else
		{
			View::make();
		}
	}

	/*
     * 创建内容页生成配置缓存
     * 结构如下：
     * array(
     * //生成的栏目数据
	 * 'cat'=>array(
	 *	1=>0,//1号栏目＝>从这个id开始生成
	 *	2=0
	 * ),
	 * 'row'=>2//每页生成行数
     * );
     */
    public function CreateArticleConfig()
    {
    	if(IS_POST)
    	{
    		$cat=Db::table('category')->lists('id');
    		$data=array();
    		foreach ($cat as $is)
    		{
    			$data[$id]=0;
    		}
    		$cache['cat']=$data;
    		$cache['row']=$_POST['row'];
    		F('__HTML__',$cache);
    		go('CreateArticle');
    	}
    	else
    	{
    		View::make();
    	}
    }

    //生成内容页
    public function CreateArticle()
    {
    	$cache=F('__HTML__');

    	if(empty($cache['cat']))
    	{
    		//所有栏目都生成完了，删除缓存结束任务
    		F("__HTML__",'[del]');
    		$this->success('全部生成完毕',U('CreateArticleConfig'));
    	}
    	//home模块控制器
    	$obj= new \Home\Controller\IndexController;
    	//读栏目
    	foreach ($cache['cat'] as $catid=>$first)
        {
            $catname = Db::table('category')->where('id',$catid)->pluck('catname');
            //本次更新文章
            $article = Db::table('article a')
            ->join('category c','a.cid','=','c.id')
            ->where('cid', $catid)
               ->limit($first,$cache['row'])
                ->get();
            //如果取不到文章，删除这个栏目在缓存中的数据
            if(empty($article))
            {
                unset($cache['cat'][$catid]);
                F('__HTML__',$cache);
                $this->success($catname.'生成完毕',U('createArticle'));
            }

            //遍历文章生成静态
            foreach ((array)$article as $a)
            {
                $date = getdate($a['addtime']);
                //静态文章地址
                $archtml = 'h/' . str_replace(
                        array('{dir}', '{y}', '{m}', '{d}', '{id}'), array($a['dir'], $date['year'], $date['mon'], $date['mday'], $a['id']), $a['archtml']
                    );
                $dir     = dirname($archtml);
                is_dir($dir) or mkdir($dir, 0755, true);
                //缓冲区
                ob_start();
                //因为文章需要id
                $_GET['id'] = $a['id'];
                $obj->article();
                $content = ob_get_clean();
                file_put_contents($archtml, $content);
            }
            $cache['cat'][$catid]=$cache['cat'][$catid]+$cache['row'];
            F('__HTML__',$cache);
            $this->success($a['catname'].'已经生成到'.$cache['cat'][$catid],U('createArticle'),1);
        }
    	
    }


    public function CreateCategoryConfig()
    {
        if(IS_POST)
        {
            $cat=Db::table('category')->get();
            $data=array();
            foreach ($cat as $c)
            {
                $_GET['id']=$c['id'];
                ob_start();
                $obj= new \Home\Controller\IndexController;
                $obj->category();
                ob_get_clean();
                $num=Page::getTotalPage();
                $data[$c['id']]=$num;
            }
            F('__HTML__',$data);
            go('createCategoryFile');
        }else
        {
            F('__HTML__',['del']);
            View::make();
        }
    }

    public function createCategoryFile()
    {
        //缓存生成栏目
        foreach (F('__HTML__') as $id=>$num)
        {
            //读栏目的数据
            $cat=Db::table('category')->where('id',$id)->first();
            for($i=1;$i<=$num;$i++)
            {   
                //indexController需要栏目id用于显示栏目
                $_GET['id']=$id;
                //设置当前页码
                $_GET['page']=$i;
                ob_start();
                $obj=new \Home\Controller\IndexController;
                $obj->category();
                $con=ob_get_clean();
                $file='h/'.str_replace(array('{dir}','{page}'), array($cat['dir'],$i), $cat['cathtml']);
                //检查目录
                if(!is_dir(dirname($file)))
                {
                    mkdir(dirname($file),0755,true);
                }

                file_put_contents($file, $con);

                if($i==1)
                {
                    copy($file,dirname($file).'/index.html');
                }

            }
        }
    }






}
